# -*- coding: utf-8 -*-
# @Time    : 2021/1/2 下午12:17
# @Author  : tgy
# https://leetcode-cn.com/problems/sliding-window-maximum/

class Solution:
    def maxSlidingWindow(self, nums, k: int):
        ...

        if not nums or k <= 0 or len(nums) < k:
            return []
        if k == 1:
            return nums
        nums_len = len(nums)
        result = [0] * (nums_len - k + 1)
        left_to_right = [0] * nums_len
        right_to_left = [0] * nums_len

        for i in range(nums_len):
            ...
            if i % k == 0:
                left_to_right[i] = nums[i]
            else:
                ...
                if nums[i] > left_to_right[i - 1]:
                    left_to_right[i] = nums[i]
                else:
                    left_to_right[i] = left_to_right[i - 1]

        # print(left_to_right)
        right_to_left[nums_len - 1] = nums[nums_len - 1]
        for i in range(nums_len - 2, -1, -1):
            pass
            if (i % k) == (k - 1):
                right_to_left[i] = nums[i]
            else:
                if nums[i] > right_to_left[i + 1]:
                    right_to_left[i] = nums[i]
                else:
                    right_to_left[i] = right_to_left[i + 1]

        # print(right_to_left)
        for i in range(0, nums_len - k + 1):
            ...
            if i % k == 0:
                result[i] = left_to_right[i + k - 1]
            else:
                result[i] = max(right_to_left[i], left_to_right[i + k - 1])
        return result


if __name__ == '__main__':
    ...
    solution = Solution()
    window = solution.maxSlidingWindow([1, 3, -1, -3, 5, 3, 6, 7], 3)
    # window = solution.maxSlidingWindow([1, -1], 1)
    print(window)
